﻿using System;
using System.Collections.Generic;
using System.Text;
using Peekaboo2008.model;
using System.Data;
using Peekaboo2008.util;
using System.Data.SqlClient;

namespace Peekaboo2008.dao
{
    class NhomHangDAO
    {
        public static DataTable getAll()
        {
            return DataUtil.executeStore("sp_NhomHang_GetAll", null);
        }
        public static DataTable getAllOrderByName()
        {
            return DataUtil.executeStore("sp_NhomHang_GetAllOrderByName", null);
        }
        public static NhomHangModel getByID(String id)
        {
            NhomHangModel nhomHangModel = null;
            SqlParameter[] param = new SqlParameter[1];
            int i = 0;
            param[i] = new SqlParameter("id", SqlDbType.NVarChar);
            param[i++].Value = id;
            DataTable kq = DataUtil.executeStore("sp_KhuVuc_GetNhomHangByID", param);
            try
            {
                foreach (DataRow row in kq.Rows)
                {
                    nhomHangModel = new NhomHangModel();
                    nhomHangModel.Id = row["ID"].ToString();
                    nhomHangModel.Ten = row["Ten"].ToString();
                    nhomHangModel.MaNganhHang = row["Ma_nganh_hang"].ToString();
                    nhomHangModel.Thue = row["Thue"].ToString();
                    nhomHangModel.ChietKhau = row["Chiet_khau"].ToString();
                    nhomHangModel.GhiChu = row["Ghi_chu"].ToString();
                    nhomHangModel.NgayCapNhatCuoi = row["Ngay_cap_nhat_cuoi"].ToString();
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
            return nhomHangModel;
        }

        public static Boolean insert(NhomHangModel nhomHangModel)
        {
            SqlParameter[] param = new SqlParameter[5];
            int i = 0;
            param[i] = new SqlParameter("ten", SqlDbType.NVarChar);
            param[i++].Value = nhomHangModel.Ten;
            param[i] = new SqlParameter("maNganhHang", SqlDbType.NVarChar);
            param[i++].Value = nhomHangModel.MaNganhHang;
            param[i] = new SqlParameter("thue", SqlDbType.NVarChar);
            param[i++].Value = nhomHangModel.Thue;
            param[i] = new SqlParameter("ChietKhau", SqlDbType.NVarChar);
            param[i++].Value = nhomHangModel.ChietKhau;
            param[i] = new SqlParameter("ghiChu", SqlDbType.VarChar);
            param[i++].Value = nhomHangModel.GhiChu;

            return DataUtil.executeNonStore("sp_NhomHang_Insert", param);
        }

        public static Boolean update(NhomHangModel nhomHangModel)
        {
            SqlParameter[] param = new SqlParameter[6];
            int i = 0;
            param[i] = new SqlParameter("id", SqlDbType.NVarChar);
            param[i++].Value = nhomHangModel.Id;
            param[i] = new SqlParameter("ten", SqlDbType.NVarChar);
            param[i++].Value = nhomHangModel.Ten;
            param[i] = new SqlParameter("maNganhHang", SqlDbType.NVarChar);
            param[i++].Value = nhomHangModel.MaNganhHang;
            param[i] = new SqlParameter("thue", SqlDbType.NVarChar);
            param[i++].Value = nhomHangModel.Thue;
            param[i] = new SqlParameter("ChietKhau", SqlDbType.NVarChar);
            param[i++].Value = nhomHangModel.ChietKhau;
            param[i] = new SqlParameter("ghiChu", SqlDbType.VarChar);
            param[i++].Value = nhomHangModel.GhiChu;

            return DataUtil.executeNonStore("sp_NhomHang_Update", param);
        }
    }
}
